<?php
class Application_Model_DbTable_Question extends Zend_Db_Table_Abstract{
	protected $_name = 'question';
	protected $_primary = array('id_qcm','num_quest');
	
	public function fetchAllWithReponses($qcm_id){
		$repDbTable = new Application_Model_DbTable_Reponse();
		$select = $this->select();
		$select->setIntegrityCheck(false);
		
		$select	->from(array('q'=>$this->_name))
				->where('q.id_qcm = ?',$qcm_id);
		$result = $select->query()->fetchAll(PDO::FETCH_OBJ);
		foreach ($result as $quest) {
			unset($quest->reponse);
			$quest->reponses = $repDbTable->fetchAllByQuestion($qcm_id, $quest->num_quest);
		}
		return $result;
	}
	public function find($qcm_id,$num_quest){
		$select = $this->select();
		$select->setIntegrityCheck(false);
		
		$select	->from(array('q'=>$this->_name))
				->where('q.id_qcm = ?',$qcm_id)
				->where('q.num_quest = ?',$num_quest);
		return $select->query()->fetch(PDO::FETCH_OBJ);
	}
	
	public function findWithReponses($qcm_id,$num_quest){
		$repDbTable = new Application_Model_DbTable_Reponse();
		$select = $this->select();
		$select->setIntegrityCheck(false);
		
		$select	->from(array('q'=>$this->_name))
				->where('q.id_qcm = ?',$qcm_id)
				->where('q.num_quest = ?',$num_quest);
		$quest = $select->query()->fetch(PDO::FETCH_OBJ);
		$quest->reponses = $repDbTable->fetchAllByQuestion($qcm_id, $num_quest);
		return $quest;
	}
	
	public function nextID($qcm_id){
		$select = $this->select();
		
		$select ->from(array('q'=>$this->_name),'MAX(num_quest) as max')
				->where('q.id_qcm = ?',$qcm_id);
		return ($select->query()->fetch(PDO::FETCH_OBJ)->max +1);
	}
}